package com.example.demo.control;



import com.example.demo.service.impl.ImageCodeUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

@Controller
public class CodeControl {

    @GetMapping("/code")
    protected void doGet(HttpServletRequest req, HttpServletResponse resp, HttpSession httpSession) throws ServletException, IOException {
        // TODO Auto-generated method stub
        ImageCodeUtils utils =new ImageCodeUtils();
        String code = utils.createCode(4,httpSession);
        InputStream is =utils.createImage(code);
        OutputStream os =resp.getOutputStream();
        int len =0;
        byte[] buff =new byte[1024];

        while((len=is.read(buff))>0) {
            os.write(buff, 0, len);
        }
        is.close();
        os.flush();
        os.close();
  }
}
